package com.ruoyi.project.lely.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.SecurityUtils;
import com.ruoyi.project.lely.bean.dto.LyAddressDTO;
import com.ruoyi.project.lely.bean.entity.LyAddress;
import com.ruoyi.project.lely.service.LyAddressService;
import com.ruoyi.common.R;
import com.ruoyi.project.lely.constant.CommonConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author luoyong
 * @Desc: 地址信息
 * @since 2022-01-13
 */
@Api(tags = "Mini-地址信息")
@RestController
@RequestMapping("/mini/address")
public class AddressController {

    @Autowired
    private LyAddressService addressService;

    @ApiOperation("查询全部地址")
    @GetMapping("/getAll")
    public R getAll() {
        List<LyAddress> list = addressService.list(
                new LambdaQueryWrapper<LyAddress>()
                        .eq(LyAddress::getUserId, SecurityUtils.getUserId())
                        .eq(LyAddress::getDeleted, CommonConstant.DELETED)
        );
        return R.ok(list);
    }

    @ApiOperation("查询默认地址")
    @GetMapping("/getDefault")
    public R getDefault() {
        LyAddress one = addressService.getOne(
                new LambdaQueryWrapper<LyAddress>()
                        .eq(LyAddress::getUserId, SecurityUtils.getUserId())
                        .eq(LyAddress::getIsDefault, CommonConstant.DEFAULT_ADDRESS_YES)
                        .eq(LyAddress::getDeleted, CommonConstant.DELETED)
        );
        return R.ok(one);
    }

    @ApiOperation("添加地址")
    @PostMapping("/add")
    public R add(@RequestBody @Validated LyAddressDTO dto) {
        return addressService.add(dto);
    }

    @ApiOperation("删除地址")
    @PostMapping("/delete/{id}")
    public R delete(@PathVariable(value = "id") Long id) {
        return R.ok(addressService.removeById(id));
    }
}

